/**
 * 岛屿的周长
 * @USER: Administrator
 * @DATE: 2024/9/27 下午9:46
 * @VERSION: 1.0.0
 */
public class LC463 {
    public static void main(String[] args) {

    }

    public int islandPerimeter(int[][] grid) {
        int result = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] == 1) {
                    result += 4;
                    // 分别判断上下左右有没有陆地
                    // 上
                    if (i>=1 && grid[i-1][j] == 1) {
                        result--;
                    }
                    // 下
                    if (i<grid.length-1 && grid[i+1][j] == 1) {
                        result--;
                    }
                    // 左
                    if (j>=1 && grid[i][j-1] == 1) {
                        result--;
                    }
                    // 右
                    if (j<grid[i].length-1 && grid[i][j+1] == 1) {
                        result--;
                    }
                }

            }
        }
        return result;
    }

}
